package com.xtjc.recsyslog.controller;

import cn.hutool.core.util.ArrayUtil;
import com.xtjc.recsyslog.netty.command.ChargeCommand;
import com.xtjc.recsyslog.netty.entity.ChargeEntity;
import com.xtjc.recsyslog.netty.util.NettyUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

@RestController
@Slf4j
public class SendCommandController {

    /**
     * 查询所有在线充电桩
     *
     * @return
     */
    @PostMapping("/remoteCharge/getChannelAll")
    public String[] getChannelAll() {
        return NettyUtil.getChannelNames();
    }

    /**
     * 下发-读取实时监测数据命令
     *  不生效命令 未处理
     * @param chargeSn
     * @return
     */
    @PostMapping("/remoteCharge/getChargeData/{chargeSn}")
    public String getChargeData(@PathVariable("chargeSn") String chargeSn) {
        try {
            ChargeEntity charge = NettyUtil.getChannel(chargeSn);
            String[] chargeData = ChargeCommand.getChargeData("01", charge);
            charge.getChannel().writeAndFlush(chargeData);
            return "ok";
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "error";
    }

    /**
     * 下发-设置时间
     *
     * @param chargeSn
     * @return
     */
    @PostMapping("/remoteCharge/setTime/{chargeSn}")
    public String setTime(@PathVariable("chargeSn") String chargeSn) {
        try {
            ChargeEntity charge = NettyUtil.getChannel(chargeSn);
            String[] chargeData = ChargeCommand.setChargeTimeZone(charge);
            charge.getChannel().writeAndFlush(chargeData);
            return "ok";
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "error";
    }

    /**
     * 下发-充电桩工作参数设置
     *  不生效命令 未处理
     * @param chargeSn
     * @return
     */
    @PostMapping("/remoteCharge/setChargeWorkParam/{chargeSn}")
    public String setChargeWorkParam(@PathVariable("chargeSn") String chargeSn) {
        try {
            ChargeEntity charge = NettyUtil.getChannel(chargeSn);
            String[] chargeData = ChargeCommand.setChargeWorkParam(true, 100, charge);
            charge.getChannel().writeAndFlush(chargeData);
            return "ok";
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "error";
    }

    /**
     * 下发-重启
     * 不生效命令 未处理
     * @param chargeSn
     * @return
     */
    @PostMapping("/remoteCharge/restart/{chargeSn}")
    public String restart(@PathVariable("chargeSn") String chargeSn) {
        try {
            ChargeEntity charge = NettyUtil.getChannel(chargeSn);
            String[] chargeData = ChargeCommand.restart(true, charge);
            charge.getChannel().writeAndFlush(chargeData);
            return "ok";
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "error";
    }

    /**
     * 获取当前充电桩的交易流水号
     */
    @PostMapping("/remoteCharge/getTxnSeqNum/{chargeSn}")
    public String getTxnSeqNum(@PathVariable("chargeSn") String chargeSn) {
        try {
            ChargeEntity charge = NettyUtil.getChannel(chargeSn);
            return ArrayUtil.toString(charge.getTxnSeqNum());
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "error";
    }

    /**
     * 运营平台远程控制启机
     */
    @PostMapping("/remoteCharge/remoteStartCharge/{chargeSn}")
    public String remoteStartCharge(@PathVariable("chargeSn") String chargeSn) {
        try {
            ChargeEntity charge = NettyUtil.getChannel(chargeSn);
            if(charge==null){
                return "error";
            }
            String[] chargeData = ChargeCommand.remoteStartCharge(charge);
            charge.getChannel().writeAndFlush(chargeData);
            return "ok";
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "error";
    }

    /**
     * 运营平台远程停机
     *
     * @param chargeSn
     * @return
     */
    @PostMapping("/remoteCharge/remoteStopCharge/{chargeSn}")
    public String remoteStopCharge(@PathVariable("chargeSn") String chargeSn) {
        try {
            ChargeEntity charge = NettyUtil.getChannel(chargeSn);
            if(charge==null){
                return "error";
            }
            String[] chargeData = ChargeCommand.remoteStopCharge(charge);
            charge.getChannel().writeAndFlush(chargeData);
            return "ok";
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "error";
    }


    /**
     * 下发二维码前缀指令
     *
     * @param chargeSn
     * @return
     */
    @PostMapping("/remoteCharge/sendQrCodeData/{chargeSn}")
    public String sendQrCodeData(@PathVariable("chargeSn") String chargeSn) {
        try {
            ChargeEntity charge = NettyUtil.getChannel(chargeSn);
            String[] chargeData = ChargeCommand.sendQrCodeData(charge);
            charge.getChannel().writeAndFlush(chargeData);
            return "ok";
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "error";
    }


}
